package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private IStatementService statementService;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {

        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {

        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void saveItems(ArrayList<StatementItem> statementItems) {
        // 满足保存状态 -- 消费中
        StatementItem temp = statementItems.remove(statementItems.size() - 1);
        Statement statement = statementService.get(temp.getStatementId());  // 注意是取 statementId
        if (statement.getStatus() != Statement.STATUS_CONSUME){
            throw new RuntimeException("只有在消费中的订单才可以保存");
        }
        BigDecimal totalAmount = new BigDecimal("0.0");// 总金额
        BigDecimal totalQuantity = new BigDecimal("0.0");// 总数量
        // 删除之前的明细 ===== 细节点======
        statementItemMapper.deleteByStatementId(temp.getStatementId());
        // ========保存结算单明细表=======
        if (statementItems.size() > 0){
            for (StatementItem item : statementItems) {
                statementItemMapper.insert(item);
                // 总金额=（数量*单价）  再求和
                totalAmount = totalAmount.add(item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity())));
                // 总数量
                totalQuantity = totalQuantity.add(new BigDecimal(item.getItemQuantity()));
            }
        }
        // 优惠金额 不能超过总金额 ======= 细节点 =====
        if (totalAmount.compareTo(temp.getItemPrice()) < 0){
            throw new BaseException(" 优惠金额 不能超过总金额 ");
        }
        // ===更新结算单表====  跟新数据: 优惠价格、总金额、总数量
        statementService.updateAmount(statement.getId(), temp.getItemPrice(), totalAmount, totalQuantity);
    }

    @Override
    public void payStatement(Long statementId) {
        // 查结算单的 结算状态
        Statement statement = statementService.get(statementId);
        if (statement == null || statement.getStatus() != Statement.STATUS_CONSUME) {
            throw new BaseException("参数异常");
        }
        // 收款人 + 收款时间
        statement.setPayTime(new Date());
        statement.setPayeeId(ShiroUtils.getUserId());
        // 修改为结算状态
        statement.setStatus(Statement.STATUS_PAID);
        // 更新结算单
        statementService.payStatement(statement);
    }

}
